import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import ProblemCode from '../components/problem_code.vue'
import ProblemList from '../components/problem/ProblemsList.vue'
import Prob from '../components/problem/Prob.vue'
import InterviewList from '../components/interviewList.vue'
import TalkBox from "../components/TalkBox.vue";

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: './login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',

    children: [
      { path: '/welcome', component: Welcome },
      { path: '/problems', component: ProblemList },
      { path: '/prob/', component: Prob},
      { path: '/prob/:id', component: Prob},
      { path: '/interview/:id', component: ProblemCode },
      { path: '/interviewList', component: InterviewList },
	  { path: '/messageBoard', component: TalkBox}
    ]

  }
]

const router = new VueRouter({
  mode: "history",
  routes
})

//为路由对象,添加beforeEach导航守卫
router.beforeEach((to, from, next) => {
  //如果用户访问的登录页,直接放行
  if (to.path === '/login') return next()
  //从sessionStorage中获取到保存的token值
  const tokenStr = window.sessionStorage.getItem('token')
  //如果么有token,强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})


export default router
